package com.qst.mapreduce.wordcount.shiyanliu;

import java.util.Random;
import java.util.UUID;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.FileWriter;
import java.io.IOException;

public class ECommerceDataGenerator {

    private static final Random random = new Random();
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final String[] productIds = new String[50]; // 存储50个唯一商品ID
    private static final String fileName = "ecommercedata.txt"; // 输出文件名

    static {
        // 初始化商品ID数组
        for (int i = 0; i < productIds.length; i++) {
            productIds[i] = "P" + (1001 + i); // 例如：P1001, P1002, ..., P1050
        }
    }

    public static void main(String[] args) {
        int numberOfRecords = 1000; // 生成1000条数据

        try (FileWriter writer = new FileWriter(fileName)) {
            // 写入文件头（如果需要）
            // writer.write("用户ID,商品ID,浏览时间,购买数量,点击率%n");

            for (int i = 0; i < numberOfRecords; i++) {
                String userId = generateUserId();
                String productId = generateProductId();
                String browsingTime = dateFormat.format(new Date(System.currentTimeMillis() - random.nextInt(86400000))); // 昨天内的随机时间
                int purchaseQuantity = generatePurchaseQuantity();
                double clickRate = generateClickRate();

                // 写入生成的数据，格式为CSV
                writer.write(String.format("%s,%s,%s,%d,%.2f%n", userId, productId, browsingTime, purchaseQuantity, clickRate));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String generateUserId() {
        return "U" + UUID.randomUUID().toString().substring(0, 4); // 生成用户ID
    }

    private static String generateProductId() {
        return productIds[random.nextInt(productIds.length)]; // 从预定义的商品ID数组中随机选择
    }

    private static int generatePurchaseQuantity() {
        return 1 + random.nextInt(10); // 购买数量介于1到10之间
    }

    private static double generateClickRate() {
        return 0.1 + (0.9 - 0.1) * random.nextDouble(); // 点击率介于0.1到0.9之间
    }
}
